package scr 
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import ru.gosoplata.model.TmpModel;
	import ru.gosoplata.view.FooterView;
	import uicontrols.ButtonBase;
	import usr.ServiseData;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenOferta extends ScreenBase implements IScreen 
	{
		public	var btn_next:ButtonBase = new ButtonBase("next", 0x00FF00);
		public var btn_terminal:ButtonBase = new ButtonBase("but_terminal", 0x555555,TmpModel.IMG_FOLDER+"/dog_oferti/images/icon_o_terminale.png");
		public var btn_oferta:ButtonBase = new ButtonBase("but_oferta", 0x555555,TmpModel.IMG_FOLDER+"/dog_oferti/images/icon_oferta.png");
		public var btn_page1:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page2:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page3:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page4:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page5:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page6:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page7:ButtonBase = new ButtonBase("but_page", 0x111111);
		private var pages:Number = 7;
		private var pic_count:Number;
		private var header:TextField = new TextField();
		private var output_txt:TextField = new TextField();
		//private var oferta_txt:String = "";
		/* INTERFACE scr.IScreen */
		private var txtloader:URLLoader;
		private var request:URLRequest = new URLRequest();
		
		private var _footer:FooterView = new FooterView();
		
		public function ScreenOferta() 
		{
			super("oferta");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			addChild(_footer);
			
			pic_count = 0;
			createBtns();
			addChild(output_txt);
		}
		
		override public function reDraw():void
		{
			_footer.reDraw();
			
			btn_oferta.fixDownBtn();
			var text_fmt:TextFormat = TmpModel.getTextFormat(32, 0x676767,true);
			text_fmt.letterSpacing = 7;

			var oferta_fmt:TextFormat = TmpModel.getTextFormat(14, 0x676767,true);
			oferta_fmt.letterSpacing = 1;
			
			header.embedFonts = true;
			header.width = 400;
			header.defaultTextFormat = text_fmt;
			header.antiAliasType = AntiAliasType.ADVANCED;
			header.thickness = -50;
			header.multiline = false;
			header.text = "Договор-оферта";
			header.autoSize = TextFieldAutoSize.CENTER;
			header.y = 320
			header.x = stage.stageWidth / 2 - header.width / 2;
			header.selectable = false;
			addChild(header);
			
			//TODO: PROFILE определение сортира или нет профайла по ServiseData.READY (есть ли в конфиге нод про сортирных техников). костыль. Клименко отказыватся делать нормальное определение.
			//TODO: да и решать что то должно не вью!
			if (ServiseData.READY) request.url = "oferta_wc.txt";
			else request.url = "oferta.txt";
			TmpModel.debugOutput.add('******************** ' + ServiseData.READY + " * " + request.url + " ********************");
			
			txtloader = new URLLoader(request);
			txtloader.addEventListener(Event.COMPLETE, onTxtLoaded);
			txtloader.load(request);
			
			output_txt.embedFonts = true;
			output_txt.defaultTextFormat =  oferta_fmt;
			output_txt.antiAliasType = AntiAliasType.ADVANCED;
			output_txt.width = 1150;
			output_txt.height = 385;
			output_txt.x = 80;
			output_txt.y = 365;
			output_txt.multiline = true;
			output_txt.wordWrap = true;
			output_txt.selectable = false;
			output_txt.condenseWhite = true;
			showPageNum(1);
		}
		
		private function onTxtLoaded(e:Event):void 
		{
			output_txt.htmlText = txtloader.data;
		}
		
		private function createBtns():void 
		{ 			
			btn_terminal.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_terminal.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_terminal);
			pic_count++
			
			btn_oferta.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_oferta.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_oferta);
			pic_count++
			
			btn_next.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_next.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			btn_next.visible = false;
			addChild(btn_next);
			pic_count++;
			
			for (var ii:Number = 1; ii <= pages; ii++ ) {
				this["btn_page"+ii].addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
				this["btn_page"+ii].addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
				this["btn_page" + ii].btn_index = 1;
				addChild(this["btn_page" + ii]);
				pic_count++;
			}
		}	
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) 
			{				
				btn_next.x = stage.stageWidth - (btn_next.width + 20)
				btn_next.y = stage.stageHeight - (btn_next.height + 20);
				//btn_terminal.addIcon(TmpModel.IMG_FOLDER+"/dog_oferti/images/icon_o_terminale.png");
				//btn_oferta.addIcon(TmpModel.IMG_FOLDER+"/dog_oferti/images/icon_oferta.png");
				
				btn_oferta.y = btn_terminal.y = 190;
				btn_terminal.x = 50;
				btn_oferta.x = stage.stageWidth - 50 - btn_oferta.width;
				
				for (var ii:Number = 1; ii <= pages; ii++ ) {
					this["btn_page" + ii].y = btn_next.y - this["btn_page" + ii].width - 30;
					this["btn_page" + ii].x = 70 + (ii - 1) *(this["btn_page" + ii].width + 5);
					this["btn_page" + ii].btn_index = ii;
					(this["btn_page" + ii] as ButtonBase).addTextIcon(ii.toString());
				}
			}
		}
		
		private function showPageNum(num:Number):void { 
				for (var ii:Number = 1; ii <= pages; ii++ ) {
					(this["btn_page" + ii] as ButtonBase).unfixBtn();
				}
				(this["btn_page" + num] as ButtonBase).fixDownBtn();
				output_txt.scrollV = (num - 1) * 20;
		}
		
		private function pressBtn(e:Event):void {
			switch(e.target.name) 
			{
				case "but_terminal":
					TmpModel.tmpGlobalController.onClick("info");
					break;
				case "but_page":
					showPageNum((e.target as ButtonBase).btn_index);
					break;
			}
		}
	}
}